package com.gm.wholesale.module.wx.share;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.module.wx.WXConstans;
import com.gm.wholesale.module.wx.bean.ShareHtmlBean;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.List;

public class WXShareUtil {
    private static IWXAPI api;

    public static IWXAPI getIWXAPI(Context context){
        if (api==null){
            initWx(context);
        }
        return api;
    }

    public static boolean isInstallWx(Context context){
            boolean bIsWXAppInstalledAndSupported = api .isWXAppInstalled();
            if (!bIsWXAppInstalledAndSupported) {
                final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
                List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
                if (pinfo != null) {
                    for (int i = 0; i < pinfo.size(); i++) {
                        String pn = pinfo.get(i).packageName;
                        if (pn.equals("com.tencent.mm")) {
                            return true;
                        }
                    }
                }
                return false;

            }
            return true;
    }

    public static void initWx(Context context) {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(context, WXConstans.APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(WXConstans.APP_ID);

        //建议动态监听微信启动广播进行注册到微信
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // 将该app注册到微信
                api.registerApp(WXConstans.APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));

    }

    public static void sendMsg(Context context,String text){
        if (api==null){
            initWx(context);
        }
        WXTextObject textObj = new WXTextObject();
        textObj.text = text;

      //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.description = text;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());  //transaction字段用与唯一标示一个请求
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;

        //调用api接口，发送数据到微信
         api.sendReq(req);
    }

    public static void sendHtml(Context context, ShareHtmlBean shareHtmlBean){
        if (!isInstallWx(context)){
            ToastUtil.showToast("请先安装微信");
            return;
        }
        if (api==null){
            initWx(context);
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =shareHtmlBean.getHtmlUrl();

       //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title =shareHtmlBean.getTitle();
        msg.description =shareHtmlBean.getContent();
        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
       // req.transaction = buildTransaction("webpage");
        req.message =msg;
       // req.scene =mTargetScene;
       // req.userOpenId = getOpenId();
      //调用api接口，发送数据到微信
        api.sendReq(req);
    }






}
